<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Department;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class CompanyController extends Controller
{
    private $user_id;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {   
        parent::__construct();
        $this->cmpInfo = new Company();
        $this->user_id = Auth::id();
    }
    //获取
    public function get($id)
    {   
        $cmpInfo = $this->cmpInfo::find($id);
        if(!empty($cmpInfo)){
            $cmpInfo->setTitle = json_decode($cmpInfo->title);
            $cmpInfo = array_except($cmpInfo->toArray(), ['createdAt','updatedAt','title']);
            return $this->respond('get_ok',['cmpInfo'=>$cmpInfo]);
        }else{
            return $this->respond('get_error');
        }
    }
    //新增保存
    public function add(Request $request)
    {   
        $cmpName = $request->input('cmpName');
        if (empty($cmpName)) {
            return $this->respond('empty_error');
        }
        $result = $this->cmpInfo->create([
            'userId'=>Auth::id(),
            'cmpName'=>$cmpName,
            'cmpWelcome'=>$request->input('cmpWelcome'),
            'status'=>1,
        ]);
        $result = array_except($result->toArray(), ['createdAt', 'updatedAt']);
        if ($result) {
            return $this->respond('add_ok',['cmpInfo'=>$result]);
        }
        return $this->respond('add_error');
    }
    //更新保存
    public function update(Request $request, $id, $step)
    {   
        $model = $this->cmpInfo->find($id);
        if (empty($model)) {
            return $this->respond('get_error');
        }
        $model->status = intval($step);
        if($step == 1){
        	$cmpName = $request->input('cmpName');
        	$cmpWelcome = $request->input('cmpWelcome');
        	if (empty($cmpName)) {
            	return $this->respond('empty_error');
        	}
        	$model->cmpName = $cmpName;
	        $model->cmpWelcome = $cmpWelcome;
	        $result = $model->save();
        }else if($step == 2){
        	$model->cmpLogo = $request->input('cmpLogo');
        	$model->cmpBgImg = $request->input('cmpBgImg');
            //导入通讯录
            $root = Department::where('parentId', '=', null)->first();
            $child = Department::where(
                ['userId' => $this->user_id, 'depth' => 1])->first();
            if (empty($child)) {
                $child = Department::create(['name' => $model->cmpName, 'userId' => $this->user_id]);
                $child->makeChildOf($root);
            }
            $result = $model->save();
            $model->deptId = $child->id;
        }else if($step == 3){
            $result = $model->save();
        }else if($step == 4){
            $model->status = 3;
            $input = $request->input();
            if(isset($input['isMessageOn']))
                $model->isMessageOn = $request->input('isMessageOn');
            if(isset($input['isMailOn']))
                $model->isMailOn = $request->input('isMailOn');
            isset($input['cmpName']) && $model->cmpName = $input['cmpName'];
            isset($input['cmpWelcome']) && $model->cmpWelcome = $input['cmpWelcome'];
            isset($input['cmpLogo']) && $model->cmpLogo = $input['cmpLogo'];
            isset($input['cmpBgImg']) && $model->cmpBgImg = $input['cmpBgImg'];
            isset($input['title']) && $model->title = json_encode($input['title']);
            $result = $model->save();
        }
        $model = array_except($model->toArray(), ['createdAt', 'updatedAt']);
        if ($result) {
            return $this->respond('update_ok',['cmpInfo'=>$model]);
        }
        return $this->respond('update_error');
    }
    //上传图片
    public function upload(Request $request)
    {   
    	$file = $request->file('file');
    	if (!$request->hasFile('file')) {
            $return = ['code'=>500,'msg'=>'上传文件为空或无效','data'=>[]];
            return response()->json($return);
		}
		if (!$request->file('file')->isValid()){
            $return = ['code'=>500,'msg'=>$file->getErrorMessage(),'data'=>[]];
    		return response()->json($return);
		}
        if(!in_array($file->getClientOriginalExtension(),['png','jpg','jpeg'])){
            $return = ['code'=>500,'msg'=>'图片格式不允许','data'=>[]];
            return response()->json($return); 
        }
//        $file->getMaxFilesize()
        if($file->getClientSize() > 2*1024*1024){
            $return = ['code'=>500,'msg'=>'图片不允许超过2M','data'=>[]];
            return response()->json($return); 
        }
        if (!empty($file)) {
	        $newFileName = md5(time() . rand(0, 10000)) . '.' . $file->getClientOriginalExtension();
            $savePath = 'uploads/' . date('Ym', time());
            $request->file('file')->move($savePath, $newFileName);
	        
            $name = $savePath . '/' . $newFileName;
            $return = ['code'=>200,'msg'=>'上传文件成功','data'=>$name];
            return response()->json($return);
	    }
    }
}
